package com.example.administrator.daydayappmarket.recommend.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.administrator.daydayappmarket.Contacts;
import com.example.administrator.daydayappmarket.R;
import com.example.administrator.daydayappmarket.recommend.activity.RecommendMainThirdActivity;
import com.example.administrator.daydayappmarket.recommend.moduls.RecommendApps;
import com.example.administrator.daydayappmarket.software.choice.model.SoftwareChoiceTopicApps;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/12/26.
 */

public class RecommendMainFootListViewAdapter extends BaseAdapter {

    private List<RecommendApps> data;
    private Context context;

    public RecommendMainFootListViewAdapter(List<RecommendApps> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @Override
    public int getCount() {
        return data != null ? data.size() : 0;
    }

    @Override
    public RecommendApps getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_recommend_main_listview_foot, parent, false);

            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        final RecommendApps item = getItem(position);
        String icon_url = item.getIcon_url();
        if (icon_url.startsWith("http")) {
            Glide.with(context).load(icon_url).into(viewHolder.image);
        } else {
            Glide.with(context).load(Contacts.URL_BASEIMAGE + item.getIcon_url()).into(viewHolder.image);
        }

        viewHolder.name.setText(item.getName());
        viewHolder.count.setText(item.getDownload_count() + "万人下载\t\t" + item.getPackage_size() + "MB");
        viewHolder.text.setText(item.getReviews());

        viewHolder.search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context,RecommendMainThirdActivity.class);
                intent.putExtra("id",item.getId());
                intent.putExtra("type",item.getApps_type());
                context.startActivity(intent);
            }
        });
        return convertView;
    }

    static class ViewHolder {
        @InjectView(R.id.item_recommend_main_listview_foot_image)
        ImageView image;
        @InjectView(R.id.item_recommend_main_listview_foot_name)
        TextView name;
        @InjectView(R.id.item_recommend_main_listview_foot_count)
        TextView count;
        @InjectView(R.id.item_recommend_main_listview_foot_text)
        TextView text;
        @InjectView(R.id.item_recommend_main_listview_foot_search)
        RelativeLayout search;
        @InjectView(R.id.item_recommend_main_listview_foot_download)
        RelativeLayout download;

        ViewHolder(View view) {
            ButterKnife.inject(this, view);
        }
    }
}
